package com.dong.es.entity.dtocast.base;

import com.dong.es.entity.EsBaseDTO;
import com.dong.es.entity.EsViewDTO;
import com.dong.es.entity.dtocast.DTOCastMethod;
import com.dong.es.entity.searchparam.page.EsFromSizePageParam;

/**
 * 使用示例：@DTOCastAnno(sort = 910, paramFields = {"pageNo", "pageSize"}, method = DTOCastFromSize.class)
 *
 * @description: From Size 分页转换
 * @author: 陈瑞东
 * @create: 2022-06-21 14:02
 */
public class DTOCastFromSize extends DTOCastMethod {

    // 页码
    private int pageNo;
    // 每页数据量
    private int pageSize;

    @Override
    protected Integer sizeLimit() {
        return 2;
    }

    @Override
    protected Class[] typeLimit() {
        return new Class[] {Integer.class, Integer.class};
    }

    @Override
    protected void load(Object... args) {
        int pageNo = Integer.parseInt(args[0].toString());
        int pageSize = Integer.parseInt(args[1].toString());

        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    @Override
    protected void doCast(EsViewDTO viewDTO, EsBaseDTO baseDTO) {
        if (pageNo < 1) {
            pageNo = 1;
        }

        int from = (pageNo - 1) * pageSize;

        new EsFromSizePageParam(from, pageSize).addTo(baseDTO);
    }
}
